package net.tngou.crawler.drug;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import net.tngou.c4j.util.HttpUtil;
import net.tngou.cache.CacheEngine;
import net.tngou.cache.TgCacheEngine;
import net.tngou.crawler.util.StringUtil;

/**
 * 康美健康
 * @author tngou@tngou.net
 *
 */
public class COM_Km1818  extends BaseCrawler implements CrawlerDrug{
	private static final Logger log= LoggerFactory.getLogger(COM_Km1818.class);

	private String url="http://www.km1818.com/products/{}.html";
	private String turl="http://search.km1818.com/10/ajaxJson/getSKUCategory.action";
	
	

	@Override
	public JSONObject getJson(URL url) {
		log.info("请求页面:{}",url.toString());
		Document doc = HttpUtil.get(url.toString());
		if(doc==null||doc.text().indexOf("你想访问的网页不存在")>=0)return null;
		json.put("url", url.toString());
		json.put("onlinepharmacy", onlinepharmacy);
		//唯一值
		String[] uns = url.toString().split("/");
		json.put("un",uns[uns.length-1].replaceAll(".html", "-").split("-")[0]);
		//标签
					
		json.put("tag", getTags(json.getString("un")));
		
		//图片
		Elements specPiclist = doc.getElementsByClass("spec-items");
		if(specPiclist.first()!=null)
		{		
			Elements imgs = specPiclist.first().getElementsByTag("a");
			List<String> list = new ArrayList<>();
			imgs.forEach(e->{
				JSONObject rel = (JSONObject) JSON.parse(e.attr("rel"));
				
				if(rel!=null)
				{
					String src = rel.getString("largeimage");
					if(StringUtil.isNotEmpty(src))
					{
						list.add(src);
					}
				}
				
			});
			json.put("picture", list);
		}
		//价格
		Element price = doc.getElementById("productPrice");
		if(price!=null)
		{	
			String value = price.text();
			json.put("price", getPrice(value));
			
		}
//		//类型
//
		//说明书
		Element instruction = doc.getElementById("product_sonDiv4");
		if(instruction!=null)
		{
			JSONObject instructions=instructions_s(instruction.text());
			
			json.put("instructions",instructions );
			
		}
		return json;
	}
	
	public List<String> getTags(String itemid) {
		List<String> list = new ArrayList<>();
		String rs = HttpUtil.getString(turl, new HashMap<String,String>() {{put("skuId", itemid);put("channel", "B2C");}});
		if(rs!=null)
		{	
			JSONObject rjs = (JSONObject) JSON.parse(rs);
			JSONObject rso = rjs.getJSONObject("returnObject");
			if(rso!=null) {
				Collection<Object> values = rso.values();
				for (Object object : values) {
					JSONObject js=(JSONObject) object;
					list.add(js.getString("name"));		
				}
			}
			
		
		}
		return list;
		
	}
	
	
}


